package chapter07

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test15_HighLevel
 * Package: chapter07
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-28
 *          Time: 21:44
 */

object Test15_HighLevel {

  //规约计算
  def main(args: Array[String]): Unit = {

    val list = List(1, 2, 3, 4)

    //1.reduce  加法规约
    println(list.reduce((a: Int, b: Int) => a + b))
    println(list.reduce(_ + _))
    //从左往右一个一个 +
    println(list.reduceLeft(_ + _))
    //从右往左边一个一个 +
    println(list.reduceRight(_ + _))


    println("-------------------")


    val list2 = List(3, 4, 5, 8, 10)
    //默认从左到右 底层就是reduceLeft
    println(list2.reduce(_ - _)) //-24
    println(list2.reduceLeft(_ - _)) //-24
    println(list2.reduceRight(_ - _)) //6
    //不是简单的减 而是一个递归操作 3  - (4 - (5 - (8 - 10)))

    //折叠 fold
    println(list.fold(10)(_ + _)) //10 + 1 + 2 + 3 + 4
    println(list.fold(10)(_ - _)) //10 - 1 - 2 - 3 -4
    println(list2.foldRight(11)(_-_))


  }

}
